package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class RoleController {


    @Autowired
    private RoleService roleService;

    //查询
    @GetMapping("/roles")
    public Result list(String name, String label,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        System.out.println("列表查询: " + name + "" + label + "" + page + "" + pageSize);
        PageResult<Role> pageResult = roleService.list(name, label, page, pageSize);
        return Result.success(pageResult);
    }

    //删除
    @DeleteMapping("/roles/{id}")
    public Result deleteById(@PathVariable Integer id){
        System.out.println("删除部门" + id);
        roleService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/roles")
    public Result addRole(@RequestBody Role role){
        roleService.addRole(role);
        return Result.success();
    }

    @GetMapping("/roles/{id}")
    public Result getById(@PathVariable Integer id){
        System.out.println("查询id:" + id);
        Role role = roleService.getById(id);
        return Result.success(role);
    }

    @PutMapping("/roles")
    public Result update(@RequestBody Role role){
        roleService.update(role);
        return Result.success();
    }

    @GetMapping("/roles/list")
    public Result listAll(){
        return Result.success(roleService.listAll());
    }
}
